package com.wanghua.mall.secKill.scheduled;

import com.wanghua.mall.secKill.constant.RedisConstant;
import com.wanghua.mall.secKill.service.SecKillService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * 秒杀商品定时上架
 * 每天晚上3点，上架最近三天需要三天秒杀的商品
 * 当天00:00:00 - 23:59:59
 * 明天00:00:00 - 23:59:59
 * 后天00:00:00 - 23:59:59
 *
 * @author wanghua
 * @create 2021-09-11 15:47
 */
@Service
public class SecKillSkuScheduled {

    @Resource
    private SecKillService secKillService;
    @Resource
    private RedissonClient redissonClient;

    @Scheduled(cron = "0 0 3 * * ?")
    public void uploadSecKillSkuLatestThreeDay() {
        //1、重复上架无需处理   ->   幂等性处理->分布式锁
        RLock lock = redissonClient.getLock(RedisConstant.UPLOAD_LOCK);
        lock.lock(10, TimeUnit.SECONDS);
        try {
            secKillService.uploadSecKillSkuLatestThreeDay();
        } finally {
            lock.unlock();
        }
    }
}
